# coding=utf-8
import logging
class Logger():
    def __init__(self,logname,loglevel,logger):
        # 创建一个logger
        format_dict={1:logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'),
                     2: logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'),
                     3: logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'),
                     4: logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'),
                     5: logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
                     }
        logger = logging.getLogger(logger)
        logger.setLevel(logging.DEBUG)

        # 创建一个handler，用于写入日志文件
        fh = logging.FileHandler(logname)
        fh.setLevel(logging.DEBUG)

        # 再创建一个handler，用于输出到控制台
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)

        # 定义handler的输出格式
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        formatter=format_dict[int(loglevel)]
        fh.setFormatter(formatter)
        ch.setFormatter(formatter)

        # 给logger添加handler
        logger.addHandler(fh)
        logger.addHandler(ch)
    def getlog(self):
        return self.logger

logger = Logger(logname='log.txt', loglevel=1, logger="fox").getlog()